package edu.xhu.communityantiepidemicsystemback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.xhu.communityantiepidemicsystemback.dto.*;
import edu.xhu.communityantiepidemicsystemback.entity.Community;
import edu.xhu.communityantiepidemicsystemback.mapper.CommunityMapper;
import edu.xhu.communityantiepidemicsystemback.service.ICommunityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcx
 * @since 2022-06-20
 */
@Service
public class CommunityServiceImpl extends ServiceImpl<CommunityMapper, Community> implements ICommunityService {

    //分页查询全部信息
    @Override
    public IPage<Community> getAllCommunityInfo(Query query) {
        Page<Community> page=new Page<>();
        page.setCurrent(query.getPageNO());
        page.setSize(query.getPageSize());
        return this.page(page);
    }

    //模糊查询
    @Override
    public IPage<Community> fuzzyCommunityInfo(Fuzzy fuzzy) {
        Page<Community> page=new Page<>();
        page.setCurrent(fuzzy.getPageNO());
        page.setSize(fuzzy.getPageSize());
        QueryWrapper<Community> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(fuzzy.getSelect(),fuzzy.getSearchStr());
        return this.page(page,queryWrapper);
    }

    //修改
    @Override
    public boolean editCommunityInfo(UpdateCommunity updateCommunity) {
        Community community=new Community();
        BeanUtils.copyProperties(updateCommunity,community);
        community.setUpdateTime(LocalDateTime.now());
        return this.updateById(community);
    }

    //添加
    @Override
    public boolean addCommunityInfo(CreateCommunity createCommunity) {
        Community community=new Community();
        BeanUtils.copyProperties(createCommunity,community);
        community.setCreateTime(LocalDateTime.now());
        return this.save(community);
    }
    //删除
    @Override
    public boolean delCommunityInfo(Integer id) {
        return this.removeById(id);
    }
}
